package com.tyjc.unique;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Toast;

import com.tyjc.unique.net.StringResultBean;
import com.fyzk.base.BaseActivity;
import com.fyzk.activity.app.App;
import com.umeng.analytics.MobclickAgent;

import org.xutils.x;

import java.lang.reflect.Field;


/**
 * 所有Fragment的基类
 */
public class BaseFragment extends Fragment implements OnClickListener {

    protected final String TAG = this.getClass().getSimpleName();

    protected BaseActivity mActivity;
    protected App mApp;
    protected Intent intent;

//    @Override
//    public void onCreateView(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        mActivity = (BaseActivity) getActivity();
//        mApp = (App) mActivity.getApplication();
//    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {
        mActivity = (BaseActivity) getActivity();
        mApp = (App) mActivity.getApplication();
        return x.view().inject(this, inflater, container);//xutil注解;;
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(TAG);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(TAG);
    }


    public void showData() {
    }

    public void back() {

    }

    public Context getContext() {
        return mActivity;
    }


    /**
     * 取消提示dialog显示
     */
    public void cancelProgressDialog() {
//        if (progressDg != null && progressDg.isShowing()) {
//            progressDg.cancel();
//        }
    }

    /**
     * 显示提示
     */
    public void showProgressDialog() {
//        if(!getContext().isFinishing()){
//            if (progressDg == null || !progressDg.isShowing()) {
//                progressDg = CustomProgress.show(getContext(), null, false, null);
//            }
//        }
    }

    /**
     * 提示
     *
     * @param text
     */
    public void showToast(String text) {
        Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
    }

    public void showToast(int resId) {
        Toast.makeText(getActivity(), resId, Toast.LENGTH_SHORT).show();
    }

    /**
     * 点击事件处理
     */
    @Override
    public void onClick(View v) {
        if (isHidden()) {
            return;
        }
    }

    /**
     * 启动Activity
     *
     * @param cls void
     */
    public void startActivity(Class<? extends Activity> cls) {
        Intent intent = new Intent(getActivity(), cls);
        startActivity(intent);
//		@SuppressWarnings("deprecation")
//		int version = Integer.valueOf(android.os.Build.VERSION.SDK);
//		if (version >= 5) {
//			getActivity().overridePendingTransition(0, 0);
//		}
    }

    @Override
    public void onDetach() {
        super.onDetach();
        try {
            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);

        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }


    protected void onFailure(int requestCode) {
        showToast("网络异常");
    }

    ;

    protected void onFailure(int requestCode, StringResultBean resultBean) {
        showToast(resultBean.getMsg());

    }

    ;

    /**
     * 刷新数据
     */
    public void refreshData() {

    }

}
